<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>The source code</title>
  <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  <style type="text/css">
    .highlight { display: block; background-color: #ddd; }
  </style>
  <script type="text/javascript">
    function highlight() {
      document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
    }
  </script>
</head>
<body onload="prettyPrint(); highlight();">
  <pre class="prettyprint lang-js">/*
 * Copyright (c) 2016 JUI All rights reserved.
 */

;(function () {

<span id='JUI-PageForm'>    /**
</span>     * 页面表单控件,包含了增删改查功能&lt;br&gt;
     &lt;pre&gt;
     pageForm = new JUI.PageForm({
	formId:'form'
	,grid:grid
	,win:win
	,crudUrl:{
		add:addUrl
	}
});
     &lt;/pre&gt;
     * @class JUI.PageForm
     * @extends JUI.Form
     */
    JUI.Class('PageForm', {
        // 默认属性
        OPTS: {
            grid: null
            , win: null
            , crudUrl: null
            , errorHandler: function (data) {
                JUI.Msg.alert('提示', data.success);
            }
        }
<span id='global-method-constructor'>        /**
</span>         * 构造函数
         * @constructor
         * @ignore
         */
        , init: function (opts) {
            this._super(opts);
        }
<span id='JUI-PageForm-method-add'>        /**
</span>         * 添加
         */
        , add: function () {
            this.setUrl(this.getCrudUrl().add);
            this.clear();
            this.getWin().setTitle('添加');
            this.getWin().show();
        }
<span id='JUI-PageForm-method-update'>        /**
</span>         * 修改
         * @param {Object} data 表格行数据
         */
        , update: function (data) {
            this.getWin().setTitle('修改');
            this.setUrl(this.getCrudUrl().update);
            this.clear();
            this.loadData(data);
            this.getWin().show();
        }
        , fireError: function (data) {
            this.opt('errorHandler')(data);
        }
<span id='JUI-PageForm-method-del'>        /**
</span>         * 删除
         * @param {Object} data 表格行数据
         * @param {Function} resultHandler 结果处理
         */
        , del: function (data, resultHandler) {
            var self = this;
            resultHandler = resultHandler || function (data) {
                    if (data.success) {
                        self.getGrid().reload();
                    } else {
                        self.fireError(data);
                    }
                }

            JUI.Msg.confirm('确认', '确定删除这条记录?', function (r) {
                if (r) {
                    var url = self.getCrudUrl().del;
                    self.submit({
                        url: url
                        , data: data
                        , onSubmit: function () {
                        }
                        , success: function (data) {
                            resultHandler(data);
                        }
                    });
                }
            });
        }
<span id='JUI-PageForm-method-save'>        /**
</span>         * 保存
         * @param {String} opts 保存选项&lt;br&gt;
         * {onSubmit:function(data){},success:function:(data){}}
         */
        , save: function (opts) {
            opts = opts || {}
            var self = this;
            var url = this.getUrl();
            this.submit({
                url: url
                , onSubmit: function (data) {
                    if (opts.onSubmit) {
                        return opts.onSubmit.call(self, data);
                    } else {
                        return this.validate();	// 返回false终止表单提交
                    }
                }
                , success: function (data) {
                    if (data.success) {
                        if (opts.success) {
                            return opts.success.call(self, data);
                        } else {
                            self.getGrid().reload();
                            self.getWin().close();
                        }
                    } else {
                        self.fireError(data);
                    }
                }
            });

        }
<span id='JUI-PageForm-method-setUrl'>        /**
</span>         * 设置提交的url
         * @param {String} url url
         */
        , setUrl: function (url) {
            this.opt('url', url);
        }
<span id='JUI-PageForm-method-getUrl'>        /**
</span>         * 返回提交的url
         * @return 提交的url
         */
        , getUrl: function () {
            return this.opts.url;
        }
<span id='JUI-PageForm-method-getCrudUrl'>        /**
</span>         * 获取增删改查的url
         * @return 返回一个json对象,如:
         * {
		add:ctx + '/addStudent.do'
		,update:ctx + '/updateStudent.do'
		,del:ctx + '/delStudent.do'
		}
         */
        , getCrudUrl: function () {
            return this.opts.crudUrl;
        }
<span id='JUI-PageForm-method-getWin'>        /**
</span>         * 获取win控件
         */
        , getWin: function () {
            return this.opts.win;
        }
<span id='JUI-PageForm-method-getGrid'>        /**
</span>         * 获取grid控件
         */
        , getGrid: function () {
            return this.opts.grid;
        }
    }, JUI.Form);

})();</pre>
</body>
</html>
